package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.pojo.*;
import com.itheima.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/statics")
public class CountController {
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private OrderService orderService;

    @GetMapping("/dishCash")
    public DishCash dishCash(){
        DishCash dishCash=new DishCash();
        LambdaQueryWrapper<OrderDetail> wrapper=new LambdaQueryWrapper<>();
      wrapper.isNotNull(OrderDetail::getDishId);
        List<OrderDetail> list = orderDetailService.list(wrapper);
        BigDecimal bigDecimal=new BigDecimal(0);

        LambdaQueryWrapper<Category> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getType,1);
        lambdaQueryWrapper.groupBy(Category::getName);
        List<Category> categoryList = categoryService.list(lambdaQueryWrapper);
        List<DishCashVo> dishCashVoList=new ArrayList<>();

        for (Category category : categoryList) {
            DishCashVo dishCashVo=new DishCashVo();
            Long categoryId = category.getId();
            Category category1 = categoryService.getById(categoryId);
            String name = category1.getName();
            dishCashVo.setName(name);
            AtomicInteger atomicInteger=new AtomicInteger(0);
           int sum=0;
            for (OrderDetail orderDetail : list) {
                Long dishId = orderDetail.getDishId();
                Dish dish = dishService.getById(dishId);
                if(dish.getCategoryId().equals(categoryId)){
//                    BigDecimal price = dish.getPrice();
//                    BigDecimal add = bigDecimal.add(price);
                     sum+=atomicInteger.addAndGet(dish.getPrice().intValue());
//                    System.out.println(atomicInteger);


                }


            }
            dishCashVo.setValue(sum);
            dishCashVoList.add(dishCashVo);

        }
        dishCash.setDishCategoryCash(dishCashVoList);


        return dishCash;

    }
    @GetMapping("/setmealCash")
    public DishCash setmealCash(){


    DishCash dishCash=new DishCash();

    LambdaQueryWrapper<OrderDetail> setmealWrapper=new LambdaQueryWrapper<>();
    setmealWrapper.isNotNull(OrderDetail::getSetmealId);
    List<OrderDetail> setmealList = orderDetailService.list(setmealWrapper);

    LambdaQueryWrapper<Category> categoryWrapper=new LambdaQueryWrapper<>();
    categoryWrapper.eq(Category::getType,2);
    categoryWrapper.groupBy(Category::getName);
    List<Category> categoryList1 = categoryService.list(categoryWrapper);
    List<DishCashVo> dishCashVoList1=new ArrayList<>();

    for (Category category : categoryList1) {
        DishCashVo dishCashVo=new DishCashVo();
        Long categoryId = category.getId();
        Category category1 = categoryService.getById(categoryId);
        String name = category1.getName();
        dishCashVo.setName(name);
        AtomicInteger atomicInteger=new AtomicInteger(0);
        int sum=0;
        for (OrderDetail orderDetail : setmealList) {
            Long setmealId = orderDetail.getSetmealId();
//                Dish dish = dishService.getById(dishId);
            Setmeal setmeal = setmealService.getById(setmealId);

            if(setmeal.getCategoryId().equals(categoryId)){
//                    BigDecimal price = dish.getPrice();
//                    BigDecimal add = bigDecimal.add(price);
                sum+=atomicInteger.addAndGet(setmeal.getPrice().intValue());
//                    System.out.println(atomicInteger);


            }

        }
        dishCashVo.setValue(sum);
        dishCashVoList1.add(dishCashVo);
    }
    dishCash.setSetmealCash(dishCashVoList1);

    return dishCash;
}
@GetMapping("/dishNum")
public DishCash dishNum(){
        DishCash dishCash=new DishCash();
        List<DishCashVo> dishCashVoList=new ArrayList<>();
        LambdaQueryWrapper<OrderDetail> wrapper=new LambdaQueryWrapper<>();
        wrapper.isNotNull(OrderDetail::getDishId);
        List<OrderDetail> orderDetailList = orderDetailService.list(wrapper);
        List<Long> categoryList=new ArrayList<>();

    for (OrderDetail orderDetail : orderDetailList) {

//        Integer number = orderDetail.getNumber();
        Long dishId = orderDetail.getDishId();
        Dish dish = dishService.getById(dishId);
        Long categoryId = dish.getCategoryId();
//        Category category = categoryService.getById(categoryId);
        categoryList.add(categoryId);

    }

    for (Long id : categoryList) {
        DishCashVo dishCashVo=new DishCashVo();
        int sum=0;
        for (OrderDetail orderDetail : orderDetailList) {
            Integer number = orderDetail.getNumber();
            Long dishId = orderDetail.getDishId();
            Dish dish = dishService.getById(dishId);
            Long categoryId = dish.getCategoryId();
            if (categoryId.equals(id)){
                sum+=number;
            }

        }

        Category category = categoryService.getById(id);
        String categoryName = category.getName();
        dishCashVo.setName(categoryName);
        dishCashVo.setValue(sum);
        dishCashVoList.add(dishCashVo);

    }
    dishCash.setDishNum(dishCashVoList);

    return dishCash;

}
@GetMapping("/setmealNum")
public DishCash setmealNum(){
    DishCash dishCash=new DishCash();
    List<DishCashVo> dishCashVoList=new ArrayList<>();
    LambdaQueryWrapper<OrderDetail> wrapper=new LambdaQueryWrapper<>();
    wrapper.isNotNull(OrderDetail::getSetmealId);
    List<OrderDetail> orderDetailList = orderDetailService.list(wrapper);
    List<Long> categoryList=new ArrayList<>();

    for (OrderDetail orderDetail : orderDetailList) {

        Long setmealId = orderDetail.getSetmealId();
        Setmeal setmeal = setmealService.getById(setmealId);
        Long categoryId = setmeal.getCategoryId();
        categoryList.add(categoryId);

    }
    for (Long id : categoryList) {
        DishCashVo dishCashVo=new DishCashVo();
        int sum=0;
        for (OrderDetail orderDetail : orderDetailList) {
            Integer number = orderDetail.getNumber();
            Long setmealId = orderDetail.getSetmealId();
            Setmeal setmeal = setmealService.getById(setmealId);
            Long categoryId = setmeal.getCategoryId();
            if (categoryId.equals(id)){
                sum+=number;
            }

        }

        Category category = categoryService.getById(id);
        String categoryName = category.getName();
        dishCashVo.setName(categoryName);
        dishCashVo.setValue(sum);
        dishCashVoList.add(dishCashVo);

    }
    dishCash.setSetmealNum(dishCashVoList);

    return dishCash;


}

@GetMapping("/cashPerMonth")
public DishCash cashPerMonth(){
        DishCash dishCash=new DishCash();
        List<Integer> list=new ArrayList<>();
        List<String> list1=new ArrayList<>();
    List<Orders> ordersList = orderService.list();
    List<String> timeList=new ArrayList<>();
    for (Orders orders : ordersList) {
        LocalDateTime orderTime = orders.getOrderTime();
        LocalDate localDate = orderTime.toLocalDate();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String time = localDate.format(dateTimeFormatter);
        timeList.add(time);
    }



    for (Orders orders : ordersList) {
        LocalDateTime orderTime = orders.getOrderTime();
        LocalDate localDate = orderTime.toLocalDate();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String time = localDate.format(dateTimeFormatter);
        String[] strings1 = time.split("-");
        for (String s : timeList) {
            String[] strings = s.split("-");
            if (strings1[0].equals(strings[0])){
                int sum=0;
                String s1=strings1[0]+"-"+strings1[1];
                AtomicInteger atomicInteger=new AtomicInteger(0);
                if (strings1[1].equals(strings[1])){
                    BigDecimal amount = orders.getAmount();
                    int i = atomicInteger.addAndGet(amount.intValue());
                    sum+=i;


                }
                list1.add(s1);
                list.add(sum);

            }

        }


    }

    dishCash.setDataList(list);
    dishCash.setMonthList(list1);





    return dishCash;
}

}
